<?php
header("content-type:text/html;charset=utf-8");

include_once "0.1conn.php"; //连接数据库

session_start();
$shop_id = $_SESSION['user']['shop_id'];
$name = $_SESSION['user']['name'];

$good_id = $_POST['goodid'];
$number = $_POST['number'];
$price = $_POST['price'];
$discount = $_POST['discount'];

//更改库存
$query = "UPDATE clothes SET num=num-$number WHERE good_id = '$good_id' AND shop_id = '$shop_id'";
mysqli_query($conn, $query);

//计算总价格
$price_i = intval($price);
$number_i = intval($number);
$discount_f = (float)$discount;
$total_price_f = $price_i * $number_i * $discount_f;
$total_price = (string)$total_price_f;

//写入销售单
$date = date('Y/m/d H:i:s');
$query = "INSERT INTO sale_record (good_id,num,total_price,salesman,time,shop_id)
        values ('$good_id','$number','$total_price','$name','$date','$shop_id')";
mysqli_query($conn, $query);

echo json_encode(array('status' => '售货成功！已更改库存并写入销售单'));
mysqli_close($conn);
